import isNaN from 'lodash/isNaN';

class ProgressFix{
    oldProgress=0;
    delta=1;
    fixed=2;
    fixSimple(progress){
        if ((typeof progress==='number')&&!isNaN(progress)) {
            let _progress= Math.abs(progress);
            _progress=Math.floor(_progress<1?_progress*100:_progress);
            this.oldProgress=_progress;
        }
        return this.oldProgress;
    }
    fix(progress){
        const {oldProgress,delta,fixed}=this;
        let _progress=this.fixSimple(progress);
        if(_progress>oldProgress){
            this.oldProgress=_progress;
        }else if(_progress<oldProgress){
            _progress=(oldProgress+delta)>98?98:(oldProgress+delta);
            this.oldProgress=_progress;
        }
        return this.oldProgress.toFixed(fixed);
    }
}
export const genProgressFix=function({
    delta=1,
    fixed=1
}={}){
    const progressFix=new ProgressFix();
    progressFix.delta=delta;
    progressFix.fixed=fixed;
    return (progress)=>{
        return progressFix.fix(progress);
    };
};
export const genProgressFixSimple=function(){
    const progressFix=new ProgressFix();
    return (progress)=>{
        return progressFix.fixSimple(progress);
    };
};
export default genProgressFix;